#ifndef GLOBAL_H
#define GLOBAL_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "hal_defs.h"

#define tm1650 1
#ifdef FAN_PWM
#define CfglistNum 33
#else
#define CfglistNum 32
#endif
#define TestlistNum 6

#define NoColdMode 1
#define GID1 0x68
#define GID2 0x6A
#define GID3 0x6C
#define GID4 0x6E

#define Num_0 0x3F
#define Num_1 0x06
#define Num_2 0x5B
#define Num_3 0x4F
#define Num_4 0x66
#define Num_5 0x6D
#define Num_6 0x7D
#define Num_7 0x07
#define Num_8 0x7F
#define Num_9 0x6F

#define Code_y 0x40 //-
#define Code_x 0x08 //_
#define Code_P 0x73
#define Code_C 0x39
#define Code_d 0x5E
#define Code_O 0x3F
#define Code_F 0x71
#define Code_E 0x79
#define Code_c 0x58
#define Code_u 0x1C
#define Code_b 0x7C

#define Code_r 0x50
#define Code_t 0x78
#define Code_S 0x6D
#define Code_A 0x77
#define Code_L 0x38
#define Code_H 0x76
#define Code_n 0x54
#define Code_i 0x10
#define Code_q 0x67

#define Code_dp 0x80 //-

#define E0 0x793F
#define E1 0x7906
#define E2 0x795B
#define E5 0x796D
#define EL 0x7938
#define EH 0x7976
#define EC 0x7939
#define E3 0x794F // 4G故障
#define E4 0x7966 // sim卡无
#define dF 0x5E71
#define Do 0x5E5C
#define PF 0x7371
#define bL 0x7C38

#define NtcValueMax 3200U
#define NtcValueMin 60U

#define amendTimeMax 120 // 2
#define amendTemp 100   // 2

#define tSecond Second  //
#define tMinute Minute//Minute//60000UL //
#define tHour Hour//3600000UL //

#define Second 1000UL  //
#define Minute 60000UL //
#define Hour 3600000UL //

#define tempTaskCycle 100
#define CompTaskCycle 500
#define FanTaskCycle CompTaskCycle

#define AlarmTaskCycle 500

#define ErrCodelistNum 4
#define OtherAlarmBit 0xE0

#define DISPLAY 1
#define ON 1
#define OFF 0

#define REG_OFF 0
#define REG_ON 1

#define LED_OFF 0
#define LED_ON 1

#define TRUE 1
#define FALSE 0

#define ENABLE 1
#define Disable 0

#define Real 0
#define Amend 1

    /**********************************/

#define HOMEPAGE 0x00
#define SETPAGE 0x01
#define CONFIGSETPAGE 0x02

#define NtcEvt_TaskId 0
#define CompCtr_TaskId 1
#define CompDefsort_TaskId 2

#define DisplayRefurbish_TaskId 3
#define SymbolRefurbish_TaskId 4
#define CfgParaTimeout_TaskId 5

//#define RealDisplayDelay_TaskId 6
//#define DisplayTempUpdate_TaskId 7
#define TimeEventSize 6

    /*********************定时任务结构体*********************/
    typedef struct
    {
        uint16_t Time;
        uint8_t Enable;
        uint8_t Flag;
        void (*TimersHandle)(void);
    } Event_Timer;

    /*************key************/
    typedef union
    {
        uint8_t allBits;
        struct
        {
            uint8_t Set : 1;
            uint8_t Increase : 1;
            uint8_t Decrease : 1;
            uint8_t Defroste : 1;
            uint8_t Led : 1;
            uint8_t Onoff : 1;
            uint8_t Other : 2;
        } OneBit;
    } keyBuff_t;

    typedef enum
    {
        None = 0,
        Press,     // 1
        LongPress, // 0
        Release,
    } keyState_t;

    /********配置参数*********/
    typedef struct
    {
        // P1 St r1 r2 r3 r4 C0 C1 C2  C3 C4 C5 C6 C7 C8 d0 d1 d2 d3 d4 dc dt dd qf  F1 F2 F3 AH Ad AS AF
        int8_t password;
        int8_t St;    // St  设定温度
        int8_t StMin; // r1  设定温度最小值
        int8_t StMax; // r2  设定温度最大值
        int8_t rd1;   // r3  温度上回差   DP
        int8_t rd2;   // r4  温度下回差   DP

        int8_t C0; // C0 柜内传感器温度补偿 DP
        int8_t C1; // C1 化霜传感器温度补偿 DP

        int8_t FactoryTestTime;//f_defrosteCycleTime;      // C2  首次除霜周期
        int8_t CompDownTimeMin;        // C3  压缩机最小停机时间
        int8_t CompRunTimeMax;         // C4  压缩机最大开机时间
        int8_t CompFirstRunTimeMax;    // C5  压缩机首次最大开机时间
        int8_t sensorErrCompRunTime;   // C6  传感器故障时压缩机工作时间
        int8_t sensorErrCompStopTime;  // C7  传感器故障时压缩机停机时间

        int8_t defrosteType;      // d0  除霜类型（0:自然化霜，1：电加热化霜）
        int8_t defrosteDelayTime; // d1  除霜启动后延迟输出时间
        int8_t defrosteShowMode;  // d2  除霜显示
        int8_t defrosteEndTemp;   // d3  除霜终止温度
        int8_t d8;                // d4  除霜后高温报警屏蔽时间
        int8_t defrosteCycleTime; // dc  除霜周期
        int8_t defrosteTime;      // dt  除霜最长时间
        int8_t defrosteDripTime;  // dd  滴水时间

        int8_t quickFrozenTime; //速冻时间 qf
        int8_t quickFrozenTemp; //速冻时间 qt

        int8_t fanMode;       // F1风机运行模式
        int8_t fanDelayTime1; // F2 通电后风机首次启动延时
        int8_t fanDelayTime2; // F3 除霜后风机启动延时

        int8_t AH; // AH  高温报警温差
        int8_t AL; // AH  高温报警温差
        int8_t Ad; // Ad  报警延时
        int8_t AS; // AS  上电报警延时
        int8_t DP; // DP  面板显示模式

        int8_t fanpwm; // F3 除霜后风机启动延时

    } cfgPara_t;

    typedef struct
    {
        uint32_t factoryModeTimeCount;  //工厂温检房测试时间
        uint32_t CompressorDownTimeCount; //压缩机停机时间
        uint32_t CompressorRunTimeCount;  //压缩机运行时间

        uint32_t sensorErrCompRunTimeCount;  //传感器故障压缩机运行时间
        uint32_t sensorErrCompStopTimeCount; //传感器故障压缩机停机时间

        uint32_t defrosteCycleTimeCount; //周期计时
        uint32_t defrosteTimeCount;      //除霜时间
        uint32_t defrosteDripTimeCount;  //除霜滴水时间计数

        uint32_t quickFrozenTimeCount; //除霜时间

        uint32_t FanDelayCount;
        uint32_t FanDelay2Count;

        uint8_t Count5s;
        uint8_t Count2s;
        uint32_t Count30m;

        uint8_t LockCount;

        // uint8_t qf_1s_Count;

        uint32_t AS_Count;
        uint32_t d8_Count;
        uint16_t AH_Count;
        uint16_t AL_Count;

        uint16_t LPCount1; // set
        uint16_t LPCount2; //+
        uint16_t LPCount3; //-
        uint16_t LPCount4; // onof
        uint16_t LPCount5;
        uint16_t LPCount6;

    } tPara_t;

    typedef union
    {
        uint8_t allBits;
        struct
        {
            uint8_t symbol : 1;
            uint8_t dp : 1;
            uint8_t res : 6;
        } OneBit;
    } state_t;

    typedef struct
    {
        uint8_t symbol : 1;
        uint8_t dp : 1;
        uint8_t res : 6;

        int8_t min; // P0
        int8_t max;

    } cfgParaRange_t;

    typedef struct
    {
        int16_t Temp_Buff[5];
        uint16_t AdValue;
//				int16_t RT_Var;
        int16_t Temp_Average;

        uint8_t ShortCirErrCount : 4;
        uint8_t OpenCirErrCount : 4;

        uint8_t ADFirstDect : 1;
        uint8_t ShortCirErr : 1;
        uint8_t OpenCirErr : 1;
        uint8_t res : 5;

    } ntcPara_t;

    typedef union
    {
        uint8_t allBits;
        struct
        {
            uint8_t Ntc0Error : 2;
            uint8_t Ntc1Error : 2;
            uint8_t AH : 1;
            uint8_t AL : 1;
            uint8_t res : 2;
        } OneBit;
    } errState_t;

    typedef union
    {
        uint8_t allBits;
        struct
        {
            // uint8_t SendData : 1;
            // uint8_t ReceiveData : 1;
            uint8_t Key1_HoldStart : 1;
            uint8_t KeyScan : 1;
            uint8_t LongScan : 1;
            uint8_t PressFlutterFree : 1;
            uint8_t ReleaseFlutterFree : 1;
            uint8_t WriteEEP : 1;
            // uint8_t LedStatus : 1;
            uint8_t Others : 2;
        } OneBit;
    } flag_t;

    typedef union
    {
        uint32_t allBits;
        struct
        {
            uint8_t AlarmPowerOnDelay : 1; //上电报警延时

            uint8_t CompFirstRun : 1;      //压缩机上电延时启动标志
            uint8_t CompFirstRunStart:1;
            uint8_t QuickFrozenEnd : 1;  //压缩机首次强制运行后除霜
            uint8_t PretendDisplay : 1;    //数据假显示标志
            uint8_t Rt0Set : 1;
            uint8_t Blink : 1;
            uint8_t LongPressStatus : 2;

            uint8_t PwRight : 1;
            uint8_t SensorErr : 1;
            uint8_t RealDisplayDelay : 1;
            uint8_t DefrostFinish : 1;
            uint8_t Alarmd8Delay : 2; //除霜后，如果有高温报警，不会立即产生高温报警，需要等待d8时间后才开始报警。(高温屏蔽)
            uint8_t AfterDefrosteFanRun : 1;

            uint8_t FanPowerOnDelay : 1;
            uint8_t FanDefrosteDelay : 1;

            uint8_t AlarmSymolBlink : 1;
            uint8_t FactoryModeSymolBlink : 1;
            uint8_t Lock: 1;
            uint16_t Others : 11;
        } OneBit;
    } flag2_t;

    typedef union
    {
        uint16_t allBits;
        struct
        {
            uint8_t TestMode : 1;
            uint8_t factoryMode : 1;
            uint8_t factoryModePart : 1;
            uint8_t pressCount : 3; 

            uint8_t defrosteTempShow : 1;
            uint8_t keyType : 1;
            uint8_t regState : 1;

            uint8_t DisplayState : 1;
            uint8_t eepThcfg : 1;
            uint8_t keyStCfg : 1;
            uint8_t eepOnoff : 1;
            uint8_t BlinkCount : 3;
        } OneBit;
    } flag3_t;

    /***********************Page**************************/
    typedef struct
    {
        uint8_t m_Type;   //页面类型：0x00主页面   0x01配置页面  0x02配置内容页面  03测试页面   04测试页面
        uint8_t m_HomePg; //记录当前所处主页的第几页（0-N）
        uint8_t m_CfgPg;  //记录当前所处配置页面的第几页(P0 --- F1)
    } tpage_MSG;

    /**工作状态**/
    typedef enum
    {
        Stopping = 0,
        Ready,   // 1
        Running, // 2
        Defrosting,
        Defrost_Dripping,
        QuickFrozening // 2
    } workState_t;

    /**速冻状态**/
    typedef enum
    {
        q_Idle = 0,
        q_Ready,       // 1
        quickFrozening // 2
    } quickFrozenState_t;

    /**工作状态**/
    typedef enum
    {
        NoAlarm = 0,
        TempAlarm,
        ErrAlarm, // 1
        //	Setting   //2
    } alarmState_t;

    typedef enum
    {
        defrostingCycle = 0,
        defrosting,
        defrost_Dripping,
    } defrostState_t;

    typedef union
    {
        uint8_t allBits;
        struct
        {
            // uint8_t Cold : 1;
            // uint8_t Defrost : 1;
            uint8_t Lock : 1;
            // uint8_t Alarm : 1;

            uint8_t Colds : 1;
            uint8_t Freeze : 1;
            uint8_t QuickFrozening : 1;
            uint8_t Others : 4;
        } OneBit;
    } displayDot_t;

#ifdef __cplusplus
}
#endif

#endif